/*
 * ChatUserInfoManager.java
 *
 * Created on 2007年5月17日, 上午10:43
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.ecomm.chat.manager;

import com.ecomm.chat.pojo.ChatUserInfo;
import com.ecomm.platcore.service.BaseEntityManager;
import com.ecomm.util.DB;
import com.ecomm.util.DBFactory;
import com.ecomm.util.HibernateUtil;
import com.ecomm.util.SpringUtil;
import com.pphome.orgmodel.manager.UserManager;
import com.pphome.orgmodel.pojo.UserInfo;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;
import org.hibernate.LockMode;
import org.hibernate.Session;


/**
 *
 * <p>Title: 易讯聊天系统</p>
 * <p>Description:
 *  易讯聊天系统
 * </p>
 * <p>Copyright: Copyright (c) 2007</p>
 * <p>Company: 联合易讯 </p>
 * @author <font color='blue'>pijunliang</font> &nbsp&nbsp<a href="mailto:pijunliang@hotmail.com">pijunliang@hotmail.com</a>
 * @version 1.0
 * @since 2007年5月17日, 上午10:43
 */
public class ChatUserInfoManager extends BaseEntityManager<ChatUserInfo> {
  
  /** Creates a new instance of ChatRemindManager */
  public ChatUserInfoManager() {
    
  }
  private static ChatUserInfoManager chatUserInfoManager = null;
  
  public static ChatUserInfoManager getInstance(){
    if(chatUserInfoManager == null){
      chatUserInfoManager = (ChatUserInfoManager)SpringUtil.getBean("chatUserInfoManager");
    }
    return chatUserInfoManager;
  }
  /**
   * 保存业务对象的函数.
   */
  public String doSaveEntity(ChatUserInfo object) {
    return (String) getHibernateTemplate().save(object);
  }
  /**
   * 保存业务对象的函数.
   * Session session = null;
   * StringBuilder sb = new StringBuilder("update ChatUserInfo set onlinestate ='");
   * sb.append(object.getOnlinestate());
   * sb.append("', userid ='");
   * sb.append(object.getUserid());
   * sb.append("', chatdesc ='");
   * sb.append(object.getChatdesc());
   * sb.append("', imgPath ='");
   * sb.append(object.getImgPath());
   * sb.append("', imgtype ='");
   * sb.append(object.getImgtype());
   * sb.append("', soundtype ='");
   * sb.append(object.getSoundtype());
   * sb.append("', fliptype ='");
   * sb.append(object.getFliptype());
   * sb.append("', mathtype ='");
   * sb.append(object.getMathtype());
   * sb.append("' where id = '");
   * sb.append(object.getId());
   * sb.append("'");
   *
   * try {
   * session = this.getSession();
   * // session.update(object);
   * session.createQuery(sb.toString()).executeUpdate();
   * } catch (Exception e) {
   * e.printStackTrace();
   * } finally {
   * if(session != null)
   * session.close();
   * }
   */
  /**
   *修改用户信息
   */
  public void doUpdateEntity(ChatUserInfo object) {
    this.getHibernateTemplate().update(object);
  }
  /**
   *通过ID得到用户的信息
   */
  public ChatUserInfo<ChatUserInfo> chatUserInfoObject(String id) {
    return (ChatUserInfo)getHibernateTemplate().get(ChatUserInfo.class,id);
  }
  /**
   * 改变用户登陆的状态
   * state 0.未登陆  1.在线
   */
  public  void updateChatUserInfoOnlineState(String  userid,String state){
    ChatUserInfo cui = null;
    if(userid != null && !userid.equals("")){
      cui = this.getChatUserInfoByUserid(userid);
      if(cui == null){
        cui = new ChatUserInfo();
        cui.setUserid(userid);
        cui.setOnlinestate(state);
        saveChatuserinfo(cui);
      }else{
        updateChatUserInfoOnlineStateS(userid,state);
      }
    }
  }
  /**
   * 改变用户登陆的状态
   * state 0.未登陆  1.在线
   */
  public  void updateChatUserInfoOnlineStateS(String  userid,String state){
    if(userid != null && !userid.equals("")){
      ChatUserInfo cu =   getChatUserInfoByUserid(userid);
      cu.setOnlinestate(state);
      this.getHibernateTemplate().update(cu);
    }
  }
  /**
   *通过HQL语句得到 对象
   *
   */
  public Object getObjectByHql(String hql){
    return this.find(hql);
  }
  /**
   *
   *保存用户信息
   */
  public void  saveChatuserinfo(ChatUserInfo chatUserInfo){
    getHibernateTemplate().save(chatUserInfo);
  }
  /**
   *根据用户ID返回用户信息配置
   */
  public  ChatUserInfo getChatUserInfoByUserid(String createUserid){
    if(createUserid== null || createUserid.equals("")){
      return null;
    }
    String hql = "from ChatUserInfo where userid = '"+createUserid+"'";
    return (ChatUserInfo)this.find(hql);
  }
  /**
   *通过用户ID得到
   *得到用户的的图片路径
   */
  public  String  getchatUserInfo(String createUserid){
    ChatUserInfo chatUserInfo = getChatUserInfoByUserid(createUserid);
    if(chatUserInfo != null){
      StringBuilder sb = new StringBuilder();
      sb.append("/chat/img/online/");
      sb.append(chatUserInfo.getImgPath());
      sb.append("_");
      sb.append(chatUserInfo.getOnlinestate());
      sb.append(".gif");
      return sb.toString();
    }else{
      return "/chat/img/online/1_0.gif";
    }
  }
  /**
   *跟前一个方法一样，
   *只是多了一个可以设置缺省的图片路径
   */
  public String  getchatUserInfo(String createUserid,String defaultReturn){
    ChatUserInfo chatUserInfo = getChatUserInfoByUserid(createUserid);
    if(chatUserInfo != null){
      StringBuilder sb = new StringBuilder();
      sb.append("/chat/img/online/");
      sb.append(chatUserInfo.getImgPath());
      sb.append("_");
      sb.append(chatUserInfo.getOnlinestate());
      sb.append(".gif");
      return sb.toString();
    }else{
      return defaultReturn;
    }
  }
  /**
   *得到当前有多少个用户再线
   */
  public String getMemberCount(String state){
    String hql = "select count(id) from ChatUserInfo where onlinestate ='"+state+"' ";
    return this.find(hql).toString();
  }
  /**
   * 校验用户和密码 是否符合
   *SELECT *
   * FROM ORGMODEL_USERINFO
   * WHERE (loginname = 'admin') AND (password = '1234')
   */
  public boolean  checkuser(String loginname,String password){
    if(loginname == null || loginname.equals("")
    || password == null || password.equals("")){
      return false;
    }
   UserInfo userInfo =  UserManager.getInstance().getuserInfoByLoginName(loginname,password);
   if(userInfo != null && !userInfo.equals("")){
     return true ;
   }else{
     return false;
   }
  }
  /**
   *修改密码
   */
  public int  changepassword(String loginname,String newpassword){
    if(loginname == null || loginname.equals("")
    || newpassword == null || newpassword.equals("")){
      return -1;
    }
    return UserManager.getInstance().changepassword(loginname,newpassword);
  }
  /**
   *覆盖父类的方法
   */
  public  Object find(String hql){
    List list =  this.getHibernateTemplate().find(hql);
    if(list != null && list.size() >0 ){
      return list.get(0);
    }else{
      return null;
    }
  }
}
